/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.view;

import br.jm.financeiro.base.eao.EaoException;
import br.jm.financeiro.datamodel.LazyDdd;
import br.jm.financeiro.entity.CepUf;
import br.jm.financeiro.entity.CodigoDdd;
import br.jm.financeiro.sistema.eao.CepUfEao;
import br.jm.financeiro.sistema.eao.CodigoDddEao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author joao
 */
public class CodigoDddBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private CodigoDdd selectedCodigoDdd;
    private LazyDataModel<CodigoDdd> lazyModel;
    private final CodigoDddEao codigoDddJpa;
    private List<CepUf> cepUfList;
    private final CepUfEao cepUfJpa;
    private Map<String, String> codUfs = new HashMap<>();
    private CepUf selectedCodUf;
    private String codUf = " ";

    public CodigoDddBean() throws EaoException {
        codigoDddJpa = CodigoDddEao.getInstance();
        cepUfJpa = CepUfEao.getInstance();
        selectedCodigoDdd = new CodigoDdd();
        cepUfList = new ArrayList<>();

        cepUfList = cepUfJpa.getNative("CepUf", 0, 30, "dscEstado", true, null, null);

        for (CepUf cepUfList1 : cepUfList) {
            selectedCodUf = cepUfList1;
            codUfs.put(selectedCodUf.getDscEstado(), selectedCodUf.getCodUf());
        }

        lazyModel = new LazyDdd(codigoDddJpa);
    }

    public void inserir(ActionEvent actionEvent) {
        FacesMessage msg = null;

        try {
            selectedCodigoDdd.setIdDdd(null);
            codigoDddJpa.create(selectedCodigoDdd);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Código D.D.D. Inserido com Sucesso: ".concat(selectedCodigoDdd.getLocalidade())),selectedCodigoDdd.getLocalidade());

        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Inserir" + selectedCodigoDdd.getLocalidade(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void alterar(ActionEvent actionEvent) {
        FacesMessage msg = null;

        try {
            codigoDddJpa.edit(selectedCodigoDdd);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Código D.D.D. Alterado com Sucesso: ".concat(selectedCodigoDdd.getLocalidade())),selectedCodigoDdd.getLocalidade());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Alterar: " + selectedCodigoDdd.getLocalidade(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void excluir(ActionEvent actionEvent) {
        FacesMessage msg = null;

        try {
            selectedCodigoDdd.getCodUf().setCodUf(this.codUf);
            codigoDddJpa.destroy(selectedCodigoDdd);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Código D.D.D. Excluido com Sucesso: ".concat(selectedCodigoDdd.getLocalidade())),selectedCodigoDdd.getLocalidade());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Excluir: " + selectedCodigoDdd.getLocalidade(),
                    e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public LazyDataModel<CodigoDdd> getLazyModel() {
        return lazyModel;
    }

    public void setLazyModel(LazyDataModel<CodigoDdd> lazyModel) {
        this.lazyModel = lazyModel;
    }

    public CodigoDdd getSelectedCodigoDdd() {
        if (selectedCodigoDdd == null) {
            selectedCodigoDdd = new CodigoDdd();
        } else {
            if (selectedCodigoDdd.getCodUf() == null) {
                CepUf cepUf = new CepUf();
                cepUf.setCodUf("SP");
                selectedCodigoDdd.setCodUf(cepUf);
            }

        }
        return selectedCodigoDdd;
    }

    public void setSelectedCodigoDdd(CodigoDdd selectedCodigoDdd) {
        if (selectedCodigoDdd == null) {
            selectedCodigoDdd = new CodigoDdd();
        } else {
            this.codUf = selectedCodigoDdd.getCodUf().getCodUf();
        }
        this.selectedCodigoDdd = selectedCodigoDdd;
    }

    public Map<String, String> getCodUfs() {
        return codUfs;
    }

    public void setCodUfs(Map<String, String> codUfs) {
        this.codUfs = codUfs;
    }

    public String getCodUf() {
        return codUf;
    }

    public void setCodUf(String codUf) {
        this.codUf = codUf;
    }

    public void preparaInserir(ActionEvent actionEvent) {
        selectedCodigoDdd = new CodigoDdd();
    }
}
